<?php

namespace app\admin\controller\shop;

use app\common\controller\Backend;
use app\common\model\Industry;
use app\common\model\Shop;
use app\common\model\ShopAudit;

/**
 * 用户打卡记录管理
 *
 * @icon fa fa-circle-o
 */
class Clock extends Backend
{
    
    /**
     * Clock模型对象
     * @var \app\common\model\Clock
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Clock;

    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(['shop.name','user.nickname'], true);

            $list = $this->model
                ->with('user,shop')
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 店铺打卡排行榜
     */
    public function shop_clock_rank(){
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            $start_time = strtotime(date('Y-m-01'));
            $end_time = strtotime(date('Y-m-01')." +1 month") - 1;

            $filter = $this->request->get("filter", '');
            $filter = (array)json_decode($filter, true);
            if(isset($filter['createtime'])){
                $start_time = strtotime($filter['createtime']."-01");
                $end_time = strtotime($filter['createtime']."-01"." +1 month") - 1;
                unset($filter['createtime']);
            }
            $this->request->get(['filter' => json_encode($filter)]);
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(['shop.name','user.nickname'], true);

            $o_where = ['clock.createtime' => ['between', [$start_time, $end_time]]];
            $fields = ['shop.id,shop.name,shop.logo,count(c.id) as clock_number,max(c.createtime) as max_time,audit.industry_id'];
            $this->model = new Shop();
            $list = $this->model->alias('shop')
                ->join('User user', 'shop.user_id = user.id')
                ->join('UserShopAudit audit', 'user.id = audit.user_id')
                ->join('Clock c', 'shop.id = c.shop_id and c.createtime BETWEEN '.$start_time.' and '.$end_time, 'left')
                ->whereNull('audit.delete_time')
                ->where('audit.status', ShopAudit::STATUS_AUDIT_PASS)
                ->where('audit.expiration_time', '>', time())
                ->where('shop.is_show', Shop::IS_SHOW_YES)
                ->where('user.status', 'normal')
                ->field($fields)
                ->order('clock_number desc,max_time asc,shop.createtime asc')
                ->group('shop.id')
                ->paginate($limit);
            /*$list = $this->model
                ->with('clock')
                ->where($where)
                ->where($o_where)
                ->field($fields)
                ->order('clock_number desc,max_time asc,shop.createtime asc')
                ->group('shop.id')
                ->paginate($limit);*/

            foreach($list as &$item){
                $item['industry_name'] = Industry::where('id', $item['industry_id'])->value('name') ?: '';
                $item['rank'] = 0;
                if($item['clock_number']){
                    $rank = model('Clock')->alias('c')->join('Shop s', 'c.shop_id = s.id')->whereNull('s.delete_time')->whereBetween('c.createtime', [$start_time, $end_time])->group('c.shop_id')->having('count(*) > '.$item['clock_number'])->select();
                    $count1 = model('Clock')->alias('c')->join('Shop s', 'c.shop_id = s.id')->whereNull('s.delete_time')->whereBetween('c.createtime', [$start_time, $end_time])->where('c.createtime', '<', $item['max_time'])->group('c.shop_id')->having('count(*) = '.$item['clock_number'])->select();
                    $item['rank'] = count($rank) + count($count1) + 1;
                }
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

}
